package com.yf.service.article.impl;

import com.yf.dao.article.ArticleDao;
import com.yf.dao.article.ThumbUserDao;
import com.yf.dao.UserDao;
import com.yf.entity.ArticleEntity;
import com.yf.entity.ThumbUserEntity;
import com.yf.entity.UserEntity;
import com.yf.response.RRException;
import com.yf.service.UserService;
import com.yf.service.article.ShareService;
import com.yf.service.impl.BaseServiceImpl;
import com.yf.vo.ShareBackVo;
import com.yf.vo.ShareVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.yf.dao.article.ShareDao;
import com.yf.entity.ShareEntity;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分享Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Aug 21 10:46:07 CST 2018
 */
@Service
public class ShareServiceImpl extends BaseServiceImpl<ShareEntity> implements ShareService {
    @Autowired
    private ShareDao shareDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private ThumbUserDao thumbUserDao;


    /**内部方法**/


    /**
     * 外部方法实现
     **/

    //通过shareId渲染文章，点击别人分享进来的
    @Override
    public ShareVo showArticle(Long shareId,Long userId) {
        ShareVo shareVo = new ShareVo();
        ShareEntity share = shareDao.get(shareId);
        UserEntity userEntity = userDao.get(share.getUserId());

        //判断是否点赞
        shareVo.setThumbUp(isThumbUp(userId,share.getArticleId()));

        //判断登录人是否分享过当前文章
        ShareEntity shareEntity = shareDao.getByUserAndArticle(userId, share.getArticleId());
        if (shareEntity == null) {
            shareVo.setUser(userEntity);
        }else {
            shareVo.setUser(userDao.get(userId));

        }

        ArticleEntity articleEntity = articleDao.get(share.getArticleId());
        shareVo.setArticle(articleEntity);

        List<UserEntity> userEntities = getLatestThumbUpUser(share.getArticleId());
        shareVo.setList(userEntities);
        return shareVo;
    }

    //通过articleId文章，从文章列表进来的
    public ShareVo showArticleByArticleId(Long articleId,Long userId) {
        ShareVo shareVo = new ShareVo();
        //判断是否点赞
        shareVo.setThumbUp(isThumbUp(userId,articleId));

        //获取用户信息
        UserEntity userEntity = userDao.get(1L);

        //判断登录人是否分享过当前文章
        ShareEntity shareEntity = shareDao.getByUserAndArticle(userId, articleId);
        if (shareEntity == null) {
            shareVo.setUser(userEntity);
        }else {
            shareVo.setUser(userDao.get(userId));

        }

        //获取文章信息
        ArticleEntity articleEntity = articleDao.get(articleId);
        shareVo.setArticle(articleEntity);
        List<UserEntity> userEntities = getLatestThumbUpUser(articleId);
        shareVo.setList(userEntities);
        return shareVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShareEntity createShare(Long userId,Long articleId,String phone,String name,String qrcode,String info) {
        //更新用户信息
        UserEntity userEntity = userDao.get(userId);
        userService.completeUserInfo(userEntity, name, info, phone, qrcode);
        //封装VO
        ArticleEntity articleEntity = articleDao.get(articleId);
        if (articleEntity == null) {
            throw new RRException("文章已经删除!");
        }

        //生成分享
        ShareEntity share = new ShareEntity(articleId, userId, new Date(), 1);
        //查询是否分享过文章
        ShareEntity shareEntity = shareDao.getByUserAndArticle(userId, articleId);
        if (shareEntity == null) {
            shareDao.save(share);
        }
        //返回生成的share
        ShareEntity shareData = shareDao.getByUserAndArticle(userId, articleId);
        return shareData;
    }

    @Override
    public Map thumbUp(Long articleId, Long userId) {
        Map map = new HashMap();
        //点赞偏移量
        int thumbCounts = 0;
        // 先判断有没有点赞
        if(isThumbUp(userId,articleId)){
            //解除点赞关系
            ThumbUserEntity thumbUserEntity = thumbUserDao.findByUserIdAndArticleId(userId,articleId);
            thumbUserDao.delete(thumbUserEntity.getId());
            thumbCounts = -1;
        }else {
            //绑定点赞关系
            ThumbUserEntity thumbUserEntity = new ThumbUserEntity(articleId, userId, new Date());
            thumbUserDao.save(thumbUserEntity);
            thumbCounts = 1;
        }
        //更新点赞数
        ArticleEntity articleEntity = articleDao.get(articleId);
        articleEntity.setThumbCount(articleEntity.getThumbCount() + thumbCounts);
        articleDao.update(articleEntity);
        map.put("thumbUps",articleEntity.getThumbCount());
        map.put("thumbUpList",getLatestThumbUpUser(articleId));
        return map;
    }

    //获取文章点赞的最新10个人
    public List<UserEntity> getLatestThumbUpUser(Long articleId){
        List<ThumbUserEntity> thumbUserEntities = thumbUserDao.queryLatestUser(articleId);
        ArrayList list = new ArrayList<>();
        for (ThumbUserEntity thumbUserEntity : thumbUserEntities) {
            list.add(thumbUserEntity.getUserId());
        }
        List<UserEntity> userEntities = new ArrayList<>();
        if(!list.isEmpty()){
            userEntities = userDao.queryLatestUser(list);
        }
        return userEntities;

    }

    //判断某个用户是否给某篇文章点赞
    public boolean isThumbUp(Long userId,Long articleId){
        ThumbUserEntity thumbUserEntity = thumbUserDao.findByUserIdAndArticleId(userId,articleId);
        return thumbUserEntity != null;
    }

    /**
     * back 分享记录
     * @param params
     * @return
     */
    @Override
    public List<ShareBackVo> findShareRecord(Map<String, Object> params) {
        List<ShareBackVo> list = shareDao.findShareRecord(params);
        return list;
    }

    @Autowired
    private UserService userService;
}
